#include "Cell.hpp"
#include "WarnException.hpp"
#include "CriticalException.hpp"
#include "Piece.hpp"

Cell::Cell(const Position& pos)
	: pos(pos), piece(0)
{}

Cell::~Cell()
{
	if (this->piece != 0)
		this->piece->setOut();
}

void Cell::setPiece(Piece* p)
{
	/*if (p == 0)
		throw new CriticalException("CELL : setPiece : piece given is NULL !");
	
	if (this->piece != 0)
		throw new WarnException("CELL : setPiece : there is already a piece on the cell !");
	*/
	this->piece = p;
	//this->piece->setCell(this);
}

void Cell::capturePiece()
{
	if (this->piece == 0)
		throw new WarnException("CELL : capturePiece : cell already cleared");

	this->piece->setOut();
	this->piece = 0;	
}

void Cell::clearPiece()
{
	this->piece = 0;
}


bool Cell::hasPiece() const
{
	return (this->piece != 0);
}

Piece* Cell::getPiece() const
{
	return (this->piece);
}

const Position&	Cell::getPosition() const
{
	return this->pos;
}

unsigned int  Cell::getColorIndex() const
{
	return (1 - ((this->pos.getRealX() + this->pos.getRealY()) % 2));
}

Chess::Color	Cell::getColor()		const
{
	if (this->getColorIndex() == 0)
		return Chess::WHITE;
	
	return Chess::BLACK;
}
